home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / SIEVEG~2.CLS < prev    next >
Text File  |  1997-06-14  |  1KB  |  38 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "GSieveGlobalP"
  6. Attribute VB_GlobalNameSpace = True
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Function SieveGlobal(ai() As Integer) As Integer
  13.     Dim iLast As Integer, cPrime As Integer, iCur As Integer, i As Integer
  14.     Dim af() As Boolean
  15.     ' Parameter should have dynamic array for maximum number of primes
  16.     If LBound(ai) <> 0 Then Exit Function
  17.     iLast = UBound(ai)
  18.     ' Create array large enough for maximum prime (initializing to zero)
  19.     ReDim af(0 To iLast + 1) As Boolean
  20.     For iCur = 2 To iLast
  21.         ' Anything still zero is a prime
  22.         If Not af(iCur) Then
  23.             ' Cancel its multiples because they can't be prime
  24.             For i = iCur + iCur To iLast Step iCur
  25.                 af(i) = True
  26.             Next
  27.             ' Count this prime
  28.             ai(cPrime) = iCur
  29.             cPrime = cPrime + 1
  30.         End If
  31.     Next
  32.     ' Resize array to the number of primes found
  33.     ReDim Preserve ai(0 To cPrime) As Integer
  34.     SieveGlobal = cPrime
  35. End Function
  36.  
  37.  
  38.